/*
 * PhraseQueryElement.java
 *
 * Created on September 8, 2007, 9:12 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.adl.registry.website.lucene;

import java.util.ArrayList;
import java.util.Iterator;

/**
 *
 * @author Jacob
 */
public class PhraseQueryElement extends InputStringQueryElement implements QueryElement
{
    private ArrayList<String> mElementParts;
    
    /** Creates a new instance of AnyTermQueryElement */
    public PhraseQueryElement()
    {
        this.mElementParts = new ArrayList<String>();
    }
    
    /** Creates a new instance of AnyTermQueryElement */
    public PhraseQueryElement(String value)
    {
        this();
        
        this.addInputValue(value);
    }
    
    public void addInputValue(String value)
    {
        //TODO: get default delimeter from properties file
        String delimeter = "\" ";
        
        String[] parts = value.split(delimeter);
        
        for (int i = 0; i < parts.length; i++)
        {
            String part = parts[i];
            
            this.mElementParts.add(part);
        }
    }

    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        
        int counter = 0;
        
        Iterator<String> iterator = this.mElementParts.iterator();
        
        while(iterator.hasNext())
        {
            String value = iterator.next();
            
            if (counter == 0)
            {
                String formatted = "\"" + value + "\"";
                
                builder.append(formatted);
            }
            else
            {
                //TODO: get default operator for portal.
                String formatted = " AND " + "\"" + value + "\"";
                
                builder.append(formatted);
            }
            
            counter++;
        }
        
        return builder.toString();
    }
    
}
